﻿#include "VethData.h"
#include "NetQtxSocket.h"
#include "common.h"


VethData::VethData(const std::string& dev, std::function<void(DataCache)> func):
    Event(0),
    m_if_mac(dev),
    m_func(func)
{

}

VethData::~VethData()
{
    shutdown();
}

int VethData::init()
{
    if (IsStart()) return -1;
    
    try
    {
        m_data_io = new NetQtxSocket(m_if_mac);
    }
    catch (...) { return -1; }

    if (0 != this->start()) 
    {
        delete m_data_io;
        return -1;
    }
    return 0;
}

int VethData::shutdown()
{
    if (IsStart()) return -1;

    try
    {
        m_data_io = new NetQtxSocket(m_if_mac);
    }
    catch (...) { return -1; }

    if (0 != this->start())
    {
        delete m_data_io;
        return -1;
    }
    return 0;
}

int VethData::VSendData(DataCache& data, int msecond)
{
    if (!m_data_io) return -1;

    return m_data_io->SendData(data, msecond);
}

void VethData::CycleTask()
{
    DataCache  data(2000);

    if (-1 == m_data_io->RecvData(data, 1000))
    {
        return;
    }
    if (data.size() == 0) return;

    if (m_func) m_func(std::move(data));
}